var app = require('express')();
var http = require('http').Server(app);
var io = require('socket.io')(http);
var netEvent = require("./netEvent");

io.on('connection', function (socket) {
    console.log('a user connected  socket.io已经连接上了！！！');
    socket.on('disconnect', function () {
        console.log('user disconnected 断开连接！！！');
    });
    /** 具体的逻辑 */
    socket.on('message', function (msg) {
        console.log('message: ' + msg);
        socket.emit('news', msg);
    });
    socket.on("join", function (msg) {
        socket.emit("ques", {
            ques: "what is this in English",
            ans: "Apple,banana,orange,I dont know"
        });
    });
    socket.on(netEvent.reqList, function (num) {
        let length = num || 5;
        let respNum = length + parseInt(Math.random() * 10);
        let data = [];
        while (typeof (respNum) == "number" && respNum > 0) {
            data.push({ num: Math.random() });
            respNum--;
        }
        console.log("reqList", data.length);
        socket.emit("reqList", {
            length: data.length,
            data: data
        });
    });
});



http.listen(3101, function () {
    console.log('listening on *:3101');
});